const app = getApp();
const Controller = require('../controllers/controllers.js').controller;
Page({
  data: {
    title:'流行慢歌',
    singnavdata: [],
    singnavdataleft:[],
    singnavdataright:[],
    singnavid:'',
    singnavpage:1,
    singnavpageSize:10,
    singnavisEnd:false,
    searchVal:''
  },
  onLoad: function (options) {
    if(options){
      this.setData({
        singnavid:options.id,
        tag:options.title
      })
      this.audioList()
      wx.setNavigationBarTitle({
        title:options.title
      })
    }
  },
  audioList(id){
    Controller.audioList({
      sortBy:0,
      categoryId:this.data.singnavid,
      page:this.data.singnavpage,
      pageSize:this.data.singnavpageSize,
      tag:this.data.tag,
    }).then(res => {
      if(res.data.baseData.list.length>0){
        for(let item of res.data.baseData.list){
          if(item.keywords){
            item.keywords=item.keywords.split(',')
          }
        }
        this.setData({
          singnavdata:this.data.singnavdata.concat(res.data.baseData.list)
        })
        let singnavdataleft=[];
        let singnavdataright=[];
        if(this.data.singnavdata && this.data.singnavdata.length>0){
          for(let i=0;i<this.data.singnavdata.length;i++){
            if(i%2==0){
              singnavdataleft.push(this.data.singnavdata[i])
            }else{
              singnavdataright.push(this.data.singnavdata[i])
            }
          }
        }
        this.setData({
          singnavdataleft:singnavdataleft,
          singnavdataright:singnavdataright
        })

        if(res.data.baseData.lastPage){
          this.setData({
            singnavisEnd:true
          })
        }
      }
    })
  },
  onReachBottom() {
    if (this.data.singnavisEnd) {
      return;
    } else {
      this.setData({
          singnavpage: this.data.singnavpage + 1,
      });
      this.audioList()
    }
  },
  // 输入
  searchValBtn(e){
    console.log(e)
    this.setData({
      searchVal:e.detail.value
    })
  },
  tosearch() {
    wx.navigateTo({
      url: '/pages/Search/Search?searchVal='+this.data.searchVal
    })
  },
  playall(e) {
    var index = 0;
    if (e.currentTarget.dataset.index) {
      index = e.currentTarget.dataset.index
    }
    app.globalData.currentIndex = index
    app.globalData.songlist = this.data.singnavdata
    wx.navigateTo({
      url: "/pages/Playing/Playing"
    })
  },
  mvlook(e) {
    wx.navigateTo({
      url: '/pages/weixinlink/weixinlink?url=' + e.currentTarget.dataset.url
    })
  },
  onShareAppMessage: function (e) {
    // if (e.from == "button") {
      let obj = {
        title: '有氧FM·流行慢歌',
        success: function (res) {
          
        },
        fail(err) {
          
        }
      }
      return obj
    // }
  },
  onShareTimeline: function (e) {
    // if (e.from == "button") {
      let obj = {
        title: '有氧FM·流行慢歌',
        success: function (res) {
          
        },
        fail(err) {
          
        }
      }
      return obj
    // }
  },
})